package org.fall.system.controller.frontstage;

import org.fall.system.annotation.AdminRoleCheckAnnotation;
import org.fall.system.constant.BackstageConstant;
import org.fall.system.model.ResultEntity;
import org.fall.system.model.dto.MemberDTO;
import org.fall.system.model.dto.MemberQueryDTO;
import org.fall.system.model.po.LeaderInfoPO;
import org.fall.system.model.vo.MemberVO;
import org.fall.system.service.MemberManagementService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 会员管理-控制器
 *
 * @author sprogFall
 * @since 2021/4/11 8:35
 */
@Controller
@RequestMapping("/member/manage/")
public class MemberManagementController {

    @Resource
    private MemberManagementService memberManagementService;

    /**
     * 查询会员list
     * @param dto dto
     * @return 查询结果
     */
    @GetMapping("list")
    @ResponseBody
    public ResultEntity getMemberList(MemberQueryDTO dto) {
        return memberManagementService.getMemberList(dto);
    }

    /**
     * 删除指定会员（软删除）
     * @param id 删除会员的id
     * @return 删除结果
     */
    @PostMapping("delete")
    @ResponseBody
    @AdminRoleCheckAnnotation(value = AdminRoleCheckAnnotation.LEVEL_TWO)
    public ResultEntity deleteMember(@RequestParam("id") Integer id) {
        return memberManagementService.deleteMember(id);
    }

    /**
     * 批量删除会员
     * @param list 删除的id的list
     * @return 删除结果
     */
    @PostMapping("batch/delete")
    @ResponseBody
    @AdminRoleCheckAnnotation(value = AdminRoleCheckAnnotation.LEVEL_TWO)
    public ResultEntity batchDeleteMembers(@RequestParam("list") List<Integer> list) {
        if (CollectionUtils.isEmpty(list)) {
            throw new RuntimeException(BackstageConstant.EMPTY_DATA);
        }
        return memberManagementService.batchDeleteMembers(list);
    }

    /**
     * 跳转编辑页面，并在request域存入详细信息
     * @param id 编辑的会员的id
     * @param modelMap modelMap
     * @return 跳转页面
     */
    @GetMapping("edit/page")
    public String toEditPageById(@RequestParam("id") Integer id, ModelMap modelMap) {
        MemberVO memberVO = memberManagementService.getMemberById(id);
        List<LeaderInfoPO> list = memberManagementService.getLeaderList();
        modelMap.put("leaders", list);
        modelMap.put("member", memberVO);
        return "pages/member/edit";
    }

    /**
     * 跳转新增页面
     * @param modelMap modelMap
     * @return 跳转页面
     */
    @GetMapping("save/page")
    public String toSavePage(ModelMap modelMap) {
        List<LeaderInfoPO> leaderList = memberManagementService.getLeaderList();
        modelMap.put("leaders", leaderList);
        return "pages/member/add";
    }

    /**
     * 更新操作
     * @param dto 更新对象
     * @return 更新结果
     */
    @PostMapping("edit")
    @ResponseBody
    @AdminRoleCheckAnnotation(value = AdminRoleCheckAnnotation.LEVEL_TWO)
    public ResultEntity editMemberInfo(@RequestBody MemberDTO dto) {
        return memberManagementService.editMemberInfo(dto);
    }

    /**
     * 新增操作
     * @param dto 新增对象
     * @return 新增结果
     */
    @PostMapping("save")
    @ResponseBody
    public ResultEntity saveMember(@RequestBody MemberDTO dto) {
        return memberManagementService.saveMember(dto);
    }


    /**
     * 被删除用户列表
     * @param dto 查询条件
     * @return 查询结果
     */
    @GetMapping("deleted/list")
    @ResponseBody
    public ResultEntity getDeletedMemberList(MemberQueryDTO dto) {
        return memberManagementService.getDeletedMemberList(dto);
    }

    /**
     * 恢复被删除的用户
     * @param id 恢复用户的id
     * @return 恢复结果
     */
    @PostMapping("deleted/restore")
    @ResponseBody
    public ResultEntity restoreDeletedMember(@RequestParam("id") Integer id) {
        return memberManagementService.restoreDeletedMember(id);
    }

    /**
     * 批量恢复被删除的用户
     * @param list 恢复用户的id list
     * @return 恢复结果
     */
    @PostMapping("deleted/batch/restore")
    @ResponseBody
    public ResultEntity batchRestoreDeletedMember(@RequestParam("list") List<Integer> list) {
        return memberManagementService.batchRestoreDeletedMember(list);
    }

    /**
     * 彻底删除用户 - 需要管理员权限≥3
     * @param id 要彻底删除的用户id
     * @return 删除结果
     */
    @PostMapping("deleted/completeDelete")
    @ResponseBody
    @AdminRoleCheckAnnotation(value = AdminRoleCheckAnnotation.LEVEL_THREE)
    public ResultEntity completeDeleteMember(@RequestParam("id") Integer id) {
        return memberManagementService.completeDeleteMember(id);
    }


}
